@use "../_variables" as *;
@use "sass:color";

.chat-page {
  display: flex;
  flex: 1;
  justify-content: center;
  overflow: hidden;
  gap: 20px;
  padding: 0 $page-h-padding;
  height: 100vh;
  padding-top: $header-height;
}

.chat-container {
  display: flex;
  flex: 1;
  justify-content: center;
  padding: 0;
  margin: 0;
  overflow: hidden;
  transition: all 0.3s ease;
  height: calc(100vh - $header-height);
  color: var(--text-medium);
}

.chat-window {
  width: 100%;
  display: flex;
  flex-direction: column;
  border: none;
  flex: 1;
  overflow: hidden;
  position: relative;

  .scroll-to-bottom-btn-container {
    position: absolute;
    width: 100%;
    max-width: 740px;
    bottom: 30px;
    left: 50%;
    transform: translate(-50%, -100%);
    display: flex;
    justify-content: flex-end;
    z-index: 1;
    pointer-events: none;
  }

  .scroll-to-bottom-btn {
    width: 30px;
    height: 30px;
    padding: 6px;
    background: var(--bg);
    border-radius: 50%;
    border: 1px solid var(--stroke-gray-medium);
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;

    &:hover {
      background: var(--bg-gray-medium);
    }

    &:focus {
      outline: none;
    }

    &.show {
      opacity: 1;
      pointer-events: auto;
    }
  }

  .chat-input-wrapper {
    bottom: 20px;
  }
}

.chat-messages-container {
  display: flex;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 15px 15px 0;
  margin-bottom: 0;
  padding-bottom: 85px;

  @include scrollbar;

  img {
    max-width: 200px;
    max-height: 200px;
    border-radius: 8px;
    object-fit: contain;
  }
}

.chat-messages-end {
  width: 100%;
  max-width: 740px;
  margin: 0 auto;
}

.message-container {
  padding: 10px 0;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  position: relative;
  max-width: $chat-width;
  margin: 0 auto;

  &:hover {
    .message-tools {
      opacity: 1;
    }
  }
}



.message {
  max-width: 100%;
  margin: 10px 0;
  padding: 10px 15px;
  border-radius: 18px;
  word-wrap: break-word;
  position: relative;

  > p {
    white-space: pre-line;
  }

  .message-images {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;

    img {
      max-width: 200px;
      max-height: 200px;
      border-radius: 8px;
      object-fit: contain;
    }
  }

  .message-video {
    min-width: 320px;
    max-width: 720px;
    max-height: 275px;
  }

  .message-table {
    width: 100%;
    border-collapse: collapse;
    border-spacing: 0;

    th, td {
      padding: 8px;
      border: 1px solid var(--stroke-gray-medium);
    }
  }

  .message-tools {
    z-index: 1;
    display: flex;
    opacity: 0;
    align-items: center;
    position: absolute;
    left: 0;
    bottom: 0;
    transform: translate(0, 100%);
    background: transparent;
    padding: 7px 0px;
    gap: 10px;
    transition: opacity $transition-normal ease;
  }

  &.sent {
    background: var(--bg-pri-medium);
    color: var(--text-invert);
    margin-left: auto;
    animation: slideFromRight $transition-normal ease;

    .message-images img {
      background: var(--bg-gray-op-medium);
    }

    .message-tools {
      left: 100%;
      transform: translate(-100%, 100%);
    }
  }

  &.received {
    width: 100%;
    animation: slideFromLeft $transition-normal ease;
    padding-left: 0;
    padding-bottom: 0;
    margin-left: 0;
    margin-bottom: 0;

    p {
      padding-left: 0;
      padding-bottom: 0;
      margin-left: 0;
      margin-bottom: 0;
    }

    .message-images img {
      background: var(--bg-gray-op-medium);
    }

    .inline-code {
      padding: 2px 3px;
      background: var(--bg-gray-medium);
    }

    pre {
      border-radius: 8px;
      overflow-x: auto;

      code {
        font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
        font-size: 14px;
        line-height: 1.4;
        white-space: pre;

        @include scrollbar;
      }
    }

    blockquote {
      margin: 10px 0;
      padding: 10px 15px;
      border-radius: 8px;
      border-left: 4px solid var(--stroke-pri-strong);
      background: var(--bg-gray-medium);

      h3 {
        margin: 0 0 10px 0;
        font-size: 16px;
        display: flex;
        align-items: center;
        gap: 8px;
      }

      pre {
        margin: 10px 0 0 0;
      }
    }

    .code-block {
      margin: 10px 0;
      border-radius: 8px;
      overflow: hidden;
      background: var(--bg-gray-medium);

      .code-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 8px 12px;
        background: var(--bg-gray-strong);

        .language {
          font-size: 12px;
          color: var(--text-medium);
        }
      }

      pre {
        margin: 0;
        padding: 12px;
        overflow-x: auto;

        @include scrollbar;

        code {
          font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
          font-size: 14px;
          line-height: 1.4;
          white-space: pre;
        }
      }
    }
  }

  &.edit {
    background: var(--bg-gray-medium);

    .edit-text {
      display: flex;
      flex-direction: column;
      gap: 15px;

      textarea {
        width: 600px;
        max-width: 100%;
        min-height: 80px;
        max-height: 200px;
        field-sizing: content;
        padding: 8px;
        border: 1px solid var(--stroke-gray-medium);
        border-radius: 8px;
        outline: none;
        resize: none;
        line-height: 20px;
        overflow-y: auto;
        background: var(--bg);
        color: var(--text-strong);

        @include scrollbar;
      }

      .edit-text-footer {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .edit-text-footer-left {
          font-size: 14px;
          color: var(--text-weak);
        }

        .edit-text-footer-right {
          display: flex;
          align-items: center;
          gap: 15px;
        }
      }
    }
  }

  .code-block-button {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-pri-medium);
    color: var(--text-invert);
    border: none;
    padding: 8px 16px;
    border-radius: 20px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 500;
    margin: 10px 0;
    transition: all 0.2s ease;

    svg {
      fill: currentColor;
    }

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px var(--sd-pri);
    }

    &:active {
      transform: translateY(0);
    }
  }

  .tool-panel {
    margin: 10px 0;
    border-radius: 8px;
    overflow: hidden;

    summary {
      cursor: pointer;
      user-select: none;
      font-weight: 500;
      color: var(--text-invert);
    }

    .tool-summary {
      display: flex;
      align-items: center;
      gap: 8px;
      cursor: pointer;
      user-select: none;
      font-weight: 500;

      .tool-summary-icon {
        width: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        transform: rotate(270deg);
        transition: transform 0.3s;
      }

      &.open {
        gap: 4px;

        .tool-summary-icon {
          transform: rotate(360deg);
          width: 12px;
          height: 12px;
          font-size: 18px;
        }
      }
    }

    .tool-panel-content {
      padding: 15px;
      border-top: 1px solid;
      max-height: 300px;
      overflow: auto;
      border-color: var(--stroke-gray-medium);

      @include scrollbar;

      pre {
        margin: 0;
        white-space: pre-wrap;
        word-break: break-all;

        @include scrollbar-light;
      }

      .tool-call-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 8px;

        .copy-btn {
          background: none;
          border: none;
          cursor: pointer;
          padding: 0;
          color: var(--text-weak);

          &:focus {
            outline: none;
          }

          &:hover {
            color: var(--text-strong);
          }
        }
      }
    }
  }
}

.code-modal {
  container-type: inline-size;
  flex: var(--code-modal-width);
  min-width: 0;
  height: 100vh;
  position: relative;

  .code-modal-content {
    height: 100%;
    background: var(--bg-dark);
    color: var(--text-invert);
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 12px var(--sd);
    border-radius: 8px 8px 0 0;

    .code-modal-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 12px 16px;
      background: var(--bg-gray-strong);
      border-radius: 8px 8px 0 0;

      .language {
        font-size: 14px;
        color: var(--text-medium);
      }

      .close-btn {
        background: none;
        border: none;
        color: var(--text-medium);
        font-size: 24px;
        cursor: pointer;
        padding: 0 8px;
        height: 22px;

        &:hover {
          color: var(--text-strong);
        }
      }

      .header-left,
      .header-right {
        display: flex;
        align-items: center;
        gap: 12px;

        .code-modal-tabs {
          display: flex;
          background: var(--bg-gray-medium);
          border-radius: 20px;
          padding: 4px;
          gap: 2px;

          .tab-btn {
            display: flex;
            align-items: center;
            gap: 6px;
            outline: none;
            background: none;
            border: none;
            color: var(--text-medium);
            padding: 6px 12px;
            cursor: pointer;
            font-size: 14px;
            border-radius: 20px;
            transition: all 0.2s;

            &:hover {
              background: var(--bg-gray-strong);
            }

            &.active {
              background: var(--bg-gray-strong);
            }

            @media (max-width: $breakpoint-m) {
              .tab-btn-text {
                display: none;
              }
            }
          }
        }
        .copy-btn {
          background: none;
          border: none;
          color: #999;
          padding: 0;
          cursor: pointer;
          font-size: 14px;
          transition: all 0.2s;

          &:hover {
            color: #fff;
          }
        }
      }
    }

    .code-modal-body {
      flex: 1;
      overflow: auto;

      @include scrollbar;

      pre {
        margin: 0 !important;
        border-radius: 0 !important;
        height: 100% !important;

        @include scrollbar;
      }

      .html-preview {
        width: 100%;
        height: 100%;
        border: none;
        background: transparent;
      }

      code {
        word-break: break-all;
        white-space: break-spaces !important;
      }
    }
  }

  @container (max-width: 768px) {
    .tab-btn-text {
      display: none;
    }
  }

  @container (max-width: 400px) {
    .language {
      display: none;
    }
  }


  .code-modal-size-box {
    position: absolute;
    top: 0;
    left: -5px;
    width: 10px;
    height: 100%;
    cursor: col-resize;

    .code-modal-size-box-icon {
      position: absolute;
      top: 50%;
      left: 0;
      transform: translateY(-50%);
      width: 8px;
      height: 24px;
      border: 1px solid var(--stroke-gray-medium);
      border-radius: 6px;
      background: var(--bg);
      z-index: 1;
      transition: all 0.2s ease;
      pointer-events: none;
    }

    &:hover {
      .code-modal-size-box-icon {
        background: var(--bg-gray-strong);
      }
    }
  }

  .code-modal-size-box-cover {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    pointer-events: none;
  }

  .code-modal-size-box-width {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    pointer-events: none;

    &.sidebar-visible {
      width: calc(100% - $sidebar-width);

      @media (max-width: $breakpoint-m) {
        width: 100%;
      }
    }
  }

  .code-modal-size-box-cover-all {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    cursor: col-resize;
    pointer-events: none;
    z-index: $z-modal;

    &.active {
      pointer-events: auto;
    }
  }

  @media (max-width: $breakpoint-xs) {
    position: fixed;
    width: 100vw;
    z-index: $z-modal;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideIn {
  from {
    right: -100%;
  }
  to {
    right: -102%;
  }
}

@keyframes slideFromRight {
  from {
    opacity: 0;
    transform: translateX(20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideFromLeft {
  from {
    opacity: 0;
    transform: translateX(-20px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.loading-dots {
  display: flex;
  gap: 4px;
  padding-top: 8px;

  span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: currentColor;
    opacity: 0.4;
    animation: loadingDot 1.4s infinite;

    &:nth-child(2) {
      animation-delay: 0.2s;
    }

    &:nth-child(3) {
      animation-delay: 0.4s;
    }
  }
}

@keyframes loadingDot {
  0%, 100% {
    opacity: 0.4;
    transform: scale(1);
  }
  50% {
    opacity: 1;
    transform: scale(1.1);
  }
}

.message-files {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;

  .message-image {
    max-width: 200px;
    max-height: 200px;
    border-radius: 8px;
    object-fit: contain;
  }

  .file-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    background: var(--bg-gray-op-medium);
    border-radius: 6px;
    font-size: 14px;

    svg {
      fill: currentColor;
    }

    .file-name {
      word-break: break-all;
    }
  }
}

.katex-block {
  padding: 1em;
  margin: 0.5em 0;
  background: var(--bg-gray-medium);
  border-radius: 6px;
  overflow-x: auto;
}

.katex-error {
  color: var(--text-danger);
  padding: 1em;
  margin: 0.5em 0;
  background: var(--bg-danger-medium);
  border-radius: 6px;
}

.think {
  padding: 1em;
  margin: 0.5em 0;
  color: var(--text-weak);
}

.markdown-link-wrapper {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  position: relative;

  .copy-link-button-wrapper {
    position: absolute;
    right: 0;
    top: 0;
    display: inline-flex;
    align-items: center;
    transform: translate(0, -100%);
    opacity: 0;
    padding-bottom: 5px;
    pointer-events: none;
    will-change: opacity, transform;
    transition: opacity 0.2s ease-in-out, transform 0.2s ease-in-out;
  }

  .copy-link-button-wrapper.show,
  &:hover .copy-link-button-wrapper,
  .copy-link-button-wrapper:hover {
    opacity: 1 !important;
    transform: translate(0, -100%) !important;
    pointer-events: auto !important;
  }

  .copy-link-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 2px;
    background: color-mix(in srgb, var(--bg-dark) 30%, transparent);
    backdrop-filter: blur(8px);
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.2s ease;
    vertical-align: middle;

    .icon-wrapper {
      display: flex;
      align-items: center;
      justify-content: center;
    }

    svg {
      display: block;
    }

    .icon-check-wrapper {
      display: none;
    }
  }

  .copy-link-button-wrapper[data-copied="true"] {
    .copy-link-button {
      .icon-link-wrapper {
        display: none;
      }

      .icon-check-wrapper {
        display: flex;
      }
    }
  }
}


.chat-authorize-popup {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;
  width: 100%;
  padding: 0 20px;

  .chat-authorize-popup-title {
    font-size: 24px;
    font-weight: 700;
  }

  .chat-authorize-popup-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 30px;
    width: 100%;

    .chat-authorize-popup-desc {
      font-size: 16px;
      font-weight: 400;
      color: var(--text-medium);
      text-align: center;
    }

    .chat-authorize-popup-tool {
      display: flex;
      gap: 10px;
      width: 100%;
      padding: 20px;
      background: var(--bg-op-dark-extremeweak);
      border-radius: 6px;
      font-size: 14px;
      color: var(--text-weak);
      border: 1px solid var(--border-op-weak);

      .chat-authorize-popup-tool-text {
        display: flex;
        flex-direction: column;
        gap: 5px;
        font-weight: 500;

        .chat-authorize-popup-tool-title {
          font-size: 15px;
          color: var(--text);
        }

        .chat-authorize-popup-tool-desc {
          font-size: 13px;
          color: var(--text-weak);
        }
      }
    }
  }
}